package cz.muni.fi.cassandra.Firebrand.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.firebrandocm.dao.PersistenceFactory;
import org.firebrandocm.dao.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import static org.firebrandocm.dao.cql.QueryBuilder.*;

import cz.muni.fi.cassandra.Firebrand.entities.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	public PersistenceFactory factory;

	public void createUser(User user) {
		if (user == null){
			throw new IllegalArgumentException("user is null");
		}
		
		factory.persist(user);
		
	}

	public User getUserById(String id) {
		if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
		
		return factory.get(User.class, id);
	}

	public void deleteUser(User user) {
		if (user == null){
			throw new IllegalArgumentException("user doesnt exist");
		}
		factory.remove(user);
	} 
	
	public List<User> findAllUsers(){
		List<User> users = new ArrayList<User>();
		users = factory.getResultList(User.class, Query.get(select(allColumns(), from(User.class))));
		return Collections.unmodifiableList(users);
	}
}
